package com.dingup.paike.controller.auxiliary;

import com.dingup.api.adapter.user.SysUserAdapter;
import com.dingup.api.base.HttpResContent;
import com.dingup.api.bean.response.SelectBean;
import com.dingup.api.model.sys.SysSubject;
import com.dingup.paike.bean.querybean.TeacherAvailableTimeQuery;
import com.dingup.paike.feign.client.subject.SubjectFeignClient;
import com.dingup.paike.service.auxiliary.AvailableTimeService;
import com.suyeer.basic.util.BHttpResUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * 辅助工具相关
 * Created by wancheng on 2018/11/30.
 */
@Controller
@RequestMapping("/auxiliary")
public class AuxiliaryController {

    @Autowired
    SubjectFeignClient subjectFeignClient;
    @Autowired
    AvailableTimeService availableTimeService;

    @RequestMapping(value = "/get_all_sys_subjects.do", method = RequestMethod.POST)
    public void getAllSubject(HttpServletRequest request,
                              HttpServletResponse response) {
        HttpResContent<List<SelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取全部指定科目");
        try {
            List<SysSubject> subjects = subjectFeignClient.getAllSysSubject();
            List<SelectBean> res = subjects.stream().map(s -> new SelectBean(s.getSubjectName(), s.getId())).collect(Collectors.toList());
            ;
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_teacher_by_subject_id.do", method = RequestMethod.POST)
    public void getTeacherBySubjectId(HttpServletRequest request,
                                      HttpServletResponse response,
                                      Integer subjectId) {
        HttpResContent<List<SelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取老师,subjectId=" + subjectId);
        try {
            List<SysUserAdapter> users = subjectFeignClient.getUserBySubjectId(subjectId);
            List<SelectBean> res = users.stream().map(u -> new SelectBean(u.getUserName(), u.getUserId())).collect(Collectors.toList());
            ;
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_teacher_available_time.do", method = RequestMethod.POST)
    public void getTeacherAvailableTime(HttpServletRequest request,
                                        HttpServletResponse response,
                                        TeacherAvailableTimeQuery query) {
        HttpResContent<List<Map<String, Object>>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("查询老师可用时间");
        try {
            List<Map<String, Object>> res = availableTimeService.getTeacherAvailableTime(query);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
}
